package com.matrix.index.ui.anime.utils

import android.content.Context
import android.content.res.AssetManager
import com.blankj.utilcode.util.GsonUtils
import com.google.gson.reflect.TypeToken
import com.matrix.index.ui.anime.bean.Root
import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader


object AssetsUtils {

    fun getAssetsData(context: Context): ArrayList<Root> {
        val inputStream: InputStream = context.assets.open("content.json")
        val reader = BufferedReader(InputStreamReader(inputStream))
        val stringBuilder = StringBuilder()
        var line: String
        while ((reader.readLine().also { line = it }) != null) {
            stringBuilder.append(line)
        }
        val jsonString = stringBuilder.toString()
        val roots: TypeToken<ArrayList<Root?>?> =
            object : TypeToken<ArrayList<Root?>?>() {}
        return GsonUtils.fromJson(jsonString, roots.type)
    }
}